#ifndef THINK_THINK_BUILD_H
#define THINK_THINK_BUILD_H

static char* think_build =
	"\n"
	"// +----------------------------------------------------------------------\n"
	"// | ThinkPHP [ WE CAN DO IT JUST THINK ]\n"
	"// +----------------------------------------------------------------------\n"
	"// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.\n"
	"// +----------------------------------------------------------------------\n"
	"// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )\n"
	"// +----------------------------------------------------------------------\n"
	"// | Author: liu21st <liu21st@gmail.com>\n"
	"// +----------------------------------------------------------------------\n"
	"\n"
	"namespace think;\n"
	"\n"
	"class Build\n"
	"{\n"
	"    /**\n"
	"     * 根据传入的 build 资料创建目录和文件\n"
	"     * @access public\n"
	"     * @param  array  $build     build 列表\n"
	"     * @param  string $namespace 应用类库命名空间\n"
	"     * @param  bool   $suffix    类库后缀\n"
	"     * @return void\n"
	"     * @throws Exception\n"
	"     */\n"
	"    public static function run(array $build = [], $namespace = \'app\', $suffix = false)\n"
	"    {\n"
	"        // 锁定\n"
	"        $lock = APP_PATH . \'build.lock\';\n"
	"\n"
	"        // 如果锁定文件不可写(不存在)则进行处理，否则表示已经有程序在处理了\n"
	"        if (!is_writable($lock)) {\n"
	"            if (!touch($lock)) {\n"
	"                throw new Exception(\n"
	"                    \'应用目录[\' . APP_PATH . \']不可写，目录无法自动生成！<BR>请手动生成项目目录~\',\n"
	"                    10006\n"
	"                );\n"
	"            }\n"
	"\n"
	"            foreach ($build as $module => $list) {\n"
	"                if (\'__dir__\' == $module) {\n"
	"                    // 创建目录列表\n"
	"                    self::buildDir($list);\n"
	"                } elseif (\'__file__\' == $module) {\n"
	"                    // 创建文件列表\n"
	"                    self::buildFile($list);\n"
	"                } else {\n"
	"                    // 创建模块\n"
	"                    self::module($module, $list, $namespace, $suffix);\n"
	"                }\n"
	"            }\n"
	"\n"
	"            // 解除锁定\n"
	"            unlink($lock);\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 创建目录\n"
	"     * @access protected\n"
	"     * @param  array $list 目录列表\n"
	"     * @return void\n"
	"     */\n"
	"    protected static function buildDir($list)\n"
	"    {\n"
	"        foreach ($list as $dir) {\n"
	"            // 目录不存在则创建目录\n"
	"            !is_dir(APP_PATH . $dir) && mkdir(APP_PATH . $dir, 0755, true);\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 创建文件\n"
	"     * @access protected\n"
	"     * @param  array $list 文件列表\n"
	"     * @return void\n"
	"     */\n"
	"    protected static function buildFile($list)\n"
	"    {\n"
	"        foreach ($list as $file) {\n"
	"            // 先创建目录\n"
	"            if (!is_dir(APP_PATH . dirname($file))) {\n"
	"                mkdir(APP_PATH . dirname($file), 0755, true);\n"
	"            }\n"
	"\n"
	"            // 再创建文件\n"
	"            if (!is_file(APP_PATH . $file)) {\n"
	"                file_put_contents(\n"
	"                    APP_PATH . $file,\n"
	"                    \'php\' == pathinfo($file, PATHINFO_EXTENSION) ? \"<?php\\n\" : \'\'\n"
	"                );\n"
	"            }\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 创建模块\n"
	"     * @access public\n"
	"     * @param  string $module    模块名\n"
	"     * @param  array  $list      build 列表\n"
	"     * @param  string $namespace 应用类库命名空间\n"
	"     * @param  bool   $suffix    类库后缀\n"
	"     * @return void\n"
	"     */\n"
	"    public static function module($module = \'\', $list = [], $namespace = \'app\', $suffix = false)\n"
	"    {\n"
	"        $module = $module ?: \'\';\n"
	"\n"
	"        // 创建模块目录\n"
	"        !is_dir(APP_PATH . $module) && mkdir(APP_PATH . $module);\n"
	"\n"
	"        // 如果不是 runtime 目录则需要创建配置文件和公共文件、创建模块的默认页面\n"
	"        if (basename(RUNTIME_PATH) != $module) {\n"
	"            self::buildCommon($module);\n"
	"            self::buildHello($module, $namespace, $suffix);\n"
	"        }\n"
	"\n"
	"        // 未指定文件和目录，则创建默认的模块目录和文件\n"
	"        if (empty($list)) {\n"
	"            $list = [\n"
	"                \'__file__\' => [\'config.php\', \'common.php\'],\n"
	"                \'__dir__\'  => [\'controller\', \'model\', \'view\'],\n"
	"            ];\n"
	"        }\n"
	"\n"
	"        // 创建子目录和文件\n"
	"        foreach ($list as $path => $file) {\n"
	"            $modulePath = APP_PATH . $module . DS;\n"
	"\n"
	"            if (\'__dir__\' == $path) {\n"
	"                // 生成子目录\n"
	"                foreach ($file as $dir) {\n"
	"                    self::checkDirBuild($modulePath . $dir);\n"
	"                }\n"
	"            } elseif (\'__file__\' == $path) {\n"
	"                // 生成（空白）文件\n"
	"                foreach ($file as $name) {\n"
	"                    if (!is_file($modulePath . $name)) {\n"
	"                        file_put_contents(\n"
	"                            $modulePath . $name,\n"
	"                            \'php\' == pathinfo($name, PATHINFO_EXTENSION) ? \"<?php\\n\" : \'\'\n"
	"                        );\n"
	"                    }\n"
	"                }\n"
	"            } else {\n"
	"                // 生成相关 MVC 文件\n"
	"                foreach ($file as $val) {\n"
	"                    $val      = trim($val);\n"
	"                    $filename = $modulePath . $path . DS . $val . ($suffix ? ucfirst($path) : \'\') . EXT;\n"
	"                    $space    = $namespace . \'\\\\\' . ($module ? $module . \'\\\\\' : \'\') . $path;\n"
	"                    $class    = $val . ($suffix ? ucfirst($path) : \'\');\n"
	"\n"
	"                    switch ($path) {\n"
	"                        case \'controller\': // 控制器\n"
	"                            $content = \"<?php\\nnamespace {$space};\\n\\nclass {$class}\\n{\\n\\n}\";\n"
	"                            break;\n"
	"                        case \'model\': // 模型\n"
	"                            $content = \"<?php\\nnamespace {$space};\\n\\nuse think\\Model;\\n\\nclass {$class} extends Model\\n{\\n\\n}\";\n"
	"                            break;\n"
	"                        case \'view\': // 视图\n"
	"                            $filename = $modulePath . $path . DS . $val . \'.html\';\n"
	"                            self::checkDirBuild(dirname($filename));\n"
	"                            $content = \'\';\n"
	"                            break;\n"
	"                        default:\n"
	"                            // 其他文件\n"
	"                            $content = \"<?php\\nnamespace {$space};\\n\\nclass {$class}\\n{\\n\\n}\";\n"
	"                    }\n"
	"\n"
	"                    if (!is_file($filename)) {\n"
	"                        file_put_contents($filename, $content);\n"
	"                    }\n"
	"                }\n"
	"            }\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 创建模块的欢迎页面\n"
	"     * @access protected\n"
	"     * @param  string $module    模块名\n"
	"     * @param  string $namespace 应用类库命名空间\n"
	"     * @param  bool   $suffix    类库后缀\n"
	"     * @return void\n"
	"     */\n"
	"    protected static function buildHello($module, $namespace, $suffix = false)\n"
	"    {\n"
	"        $filename = APP_PATH . ($module ? $module . DS : \'\') .\n"
	"            \'controller\' . DS . \'Index\' .\n"
	"            ($suffix ? \'Controller\' : \'\') . EXT;\n"
	"\n"
	"        if (!is_file($filename)) {\n"
	"            $module = $module ? $module . \'\\\\\' : \'\';\n"
	"            $suffix = $suffix ? \'Controller\' : \'\';\n"
	"            $content = str_replace(\n"
	"                [\'{$app}\', \'{$module}\', \'{layer}\', \'{$suffix}\'],\n"
	"                [$namespace, $module, \'controller\', $suffix],\n"
	"                file_get_contents(THINK_PATH . \'tpl\' . DS . \'default_index.tpl\')\n"
	"            );\n"
	"\n"
	"            self::checkDirBuild(dirname($filename));\n"
	"            file_put_contents($filename, $content);\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 创建模块的公共文件\n"
	"     * @access protected\n"
	"     * @param  string $module 模块名\n"
	"     * @return void\n"
	"     */\n"
	"    protected static function buildCommon($module)\n"
	"    {\n"
	"        $config = CONF_PATH . ($module ? $module . DS : \'\') . \'config.php\';\n"
	"\n"
	"        self::checkDirBuild(dirname($config));\n"
	"\n"
	"        if (!is_file($config)) {\n"
	"            file_put_contents($config, \"<?php\\n//配置文件\\nreturn [\\n\\n];\");\n"
	"        }\n"
	"\n"
	"        $common = APP_PATH . ($module ? $module . DS : \'\') . \'common.php\';\n"
	"        if (!is_file($common)) file_put_contents($common, \"<?php\\n\");\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 创建目录\n"
	"     * @access protected\n"
	"     * @param  string $dirname 目录名称\n"
	"     * @return void\n"
	"     */\n"
	"    protected static function checkDirBuild($dirname)\n"
	"    {\n"
	"        !is_dir($dirname) && mkdir($dirname, 0755, true);\n"
	"    }\n"
	"}\n"
	"\n"
;
#endif